package com.example.enterprisemanager.mvvm.fragment.finding;

import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.Goods;


import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class FindModel extends BaseModel {

    private Disposable mFindNameDisposable;
    private Disposable mGetNameDisposable;
    private Disposable mGetPriceDisposable;
    private Disposable mGetDepartDisposable;
    private Disposable mGetOtherDisposable;

    public void getGoodsByType(String type, Consumer<List<Goods>> success, Consumer<Exception> fail) {
        List<BmobQuery<Goods>> andQuerys = new ArrayList<BmobQuery<Goods>>();
        BmobQuery<Goods> typeQuery = new BmobQuery<>();
        typeQuery.addWhereEqualTo("type", type);

        BmobQuery<Goods> ruinQuery = new BmobQuery<>();
        ruinQuery.addWhereEqualTo("isRuin", false);

        andQuerys.add(typeQuery);
        andQuerys.add(ruinQuery);

        BmobQuery<Goods> query = new BmobQuery<>();
        query.and(andQuerys);
        mFindNameDisposable = query.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> goods, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(goods);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void getGoodsByName(String name, Consumer<List<Goods>> success, Consumer<Exception> fail) {
        List<BmobQuery<Goods>> andQuerys = new ArrayList<BmobQuery<Goods>>();
        BmobQuery<Goods> typeQuery = new BmobQuery<>();
        typeQuery.addWhereEqualTo("name", name);

        BmobQuery<Goods> ruinQuery = new BmobQuery<>();
        ruinQuery.addWhereEqualTo("isRuin", false);

        andQuerys.add(typeQuery);
        andQuerys.add(ruinQuery);

        BmobQuery<Goods> query = new BmobQuery<>();
        query.and(andQuerys);
        mGetNameDisposable = query.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> goods, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(goods);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void getGoodsByPrice(int priceStart, int priceEnd, Consumer<List<Goods>> success, Consumer<Exception> fail) {
        List<BmobQuery<Goods>> andQuerys = new ArrayList<BmobQuery<Goods>>();
        BmobQuery<Goods> moreQuery = new BmobQuery<>();
        moreQuery.addWhereGreaterThanOrEqualTo("price", priceStart);

        BmobQuery<Goods> lessQuery = new BmobQuery<>();
        lessQuery.addWhereLessThanOrEqualTo("price", priceEnd);

        andQuerys.add(moreQuery);
        andQuerys.add(lessQuery);

        BmobQuery<Goods> query = new BmobQuery<>();
        query.and(andQuerys);
        mGetPriceDisposable = query.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> goods, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(goods);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void getGoodsByDepart(String depart, Consumer<List<Goods>> success, Consumer<Exception> fail) {
        List<BmobQuery<Goods>> andQuerys = new ArrayList<BmobQuery<Goods>>();
        BmobQuery<Goods> typeQuery = new BmobQuery<>();
        typeQuery.addWhereEqualTo("department", depart);

        BmobQuery<Goods> ruinQuery = new BmobQuery<>();
        ruinQuery.addWhereEqualTo("isRuin", false);

        andQuerys.add(typeQuery);
        andQuerys.add(ruinQuery);

        BmobQuery<Goods> query = new BmobQuery<>();
        query.and(andQuerys);
        mGetDepartDisposable = query.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> goods, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(goods);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void getGoodsByOther(Consumer<List<Goods>> success, Consumer<Exception> fail) {
        List<BmobQuery<Goods>> andQuerys = new ArrayList<BmobQuery<Goods>>();
        BmobQuery<Goods> typeQuery = new BmobQuery<>();

        BmobQuery<Goods> ruinQuery = new BmobQuery<>();
        ruinQuery.addWhereEqualTo("isRuin", false);

        andQuerys.add(typeQuery);
        andQuerys.add(ruinQuery);

        BmobQuery<Goods> query = new BmobQuery<>();
        query.and(andQuerys);
        mGetOtherDisposable = query.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> goods, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(goods);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFindNameDisposable != null) {
            mFindNameDisposable.dispose();
            mFindNameDisposable = null;
        }
        if (mGetNameDisposable != null) {
            mGetNameDisposable.dispose();
            mGetNameDisposable = null;
        }
        if (mGetPriceDisposable != null) {
            mGetPriceDisposable.dispose();
            mGetPriceDisposable = null;
        }
        if (mGetDepartDisposable != null) {
            mGetDepartDisposable.dispose();
            mGetDepartDisposable = null;
        }
        if (mGetOtherDisposable != null) {
            mGetOtherDisposable.dispose();
            mGetOtherDisposable = null;
        }
    }
}
